import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_rider/api/api.dart';
import 'package:flutter_rider/kit/loc_kit.dart';
import 'package:flutter_rider/kit/tts_kit.dart';
import 'package:flutter_rider/module/mine/provider/mine_provider.dart';
import 'package:flutter_rider/routers/routers.dart';
import 'package:flutter_rider/utils/device_utils.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';


class App {
  static bool isDebugMode = false;
  //屏幕方向，true横屏 false为竖屏
  static bool isLandscape = false;

  static init() async {
    var sp = await SharedPreferences.getInstance();
    isDebugMode = sp.getBool("debug_mode") ?? false;
    isLandscape = sp.getBool("landscape") ?? false;
    if (isLandscape) {
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
    }
    TTSKit.instance.initTts();
    DeviceUtils.instance.initPlatformState();
    Routes.initRoutes();
  }

  static void setScreenLandscape(bool value) async {
    isLandscape = value;
    var sp = await SharedPreferences.getInstance();
    sp.setBool("landscape", value);
    if (value) {
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
    } else {
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
    }
  }

  ///初始化提供者数据
  static initRef(WidgetRef ref) {
    ref.read(isWorkListenProvider);
    if (Api.isLogin()) {
      ref.read(isWorkProvider.notifier).getWork();
    }
  }

  //全局的key 用来获取content
  static GlobalKey<NavigatorState> navigatorKey = GlobalKey();


  ///自定义地图mark的 widget转字节
  static Future<ByteData?> widgetToByteData(Widget widget,
      {Alignment alignment = Alignment.center,
      Size size = const Size(double.maxFinite, double.maxFinite),
      double devicePixelRatio = 1.0,
      double pixelRatio = 1.0}) async {
    RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();

    RenderView renderView = RenderView(
      child: RenderPositionedBox(alignment: alignment, child: repaintBoundary),
      configuration: ViewConfiguration(
        size: size,
        devicePixelRatio: devicePixelRatio,
      ),
      window: ui.window,
    );

    PipelineOwner pipelineOwner = PipelineOwner();
    pipelineOwner.rootNode = renderView;
    renderView.prepareInitialFrame();

    BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());
    RenderObjectToWidgetElement rootElement = RenderObjectToWidgetAdapter(
      container: repaintBoundary,
      child: widget,
    ).attachToRenderTree(buildOwner);
    buildOwner.buildScope(rootElement);
    buildOwner.finalizeTree();

    pipelineOwner.flushLayout();
    pipelineOwner.flushCompositingBits();
    pipelineOwner.flushPaint();

    ui.Image image = await repaintBoundary.toImage(pixelRatio: pixelRatio);
    ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);

    return byteData;
  }

}
